<template>
	<view class="p-2 bg-white">
		<view class="py-2 pl-20 position-relative line-h">
			<view class="position-absolute font-md" style="top:40upx;left:20upx;">
			<text class="text-danger">*</text>备件或零件：
			</view>
			<search-picker :range="allMaterialParts" 
			placeholder="请选择备件或零件" leftValue="name" rightValue="code" 
			@selectItem="selectItem"  v-model="formData.wechatMaterialName" @getSearchAll="getSearchAll"></search-picker>
		</view>
		
		<view class="py-2 pl-20 position-relative line-h">
			<view class="position-absolute font-md" style="top:40upx;left:20upx;">
			<text class="text-danger">*</text>数量：
			</view>
			<input class="border p-2 w-100 d-block line-h border-box input-defind rounded"
			 :class="focusClass.nums ? 'input-border-color' : ''"
			placeholder-class="text-light-muted" @focus="focus('nums')" @blur="blur('nums')"
			placeholder="请输入数量" type="number"
			v-model="formData.nums"/>
		</view>
		
		<view class="main-bg-color text-white rounded mt-4 font-md text-center py-2" hover-class="main-bg-hover-color" @click="submit">
			提交
		</view>
	</view>
</template>

<script>
	import searchPicker from '@/components/searchPicker/searchPicker.vue'
	import api from '@/common/api/api.js'
	export default {
		components:{
			searchPicker
		},
		data() {
			return {
				id:'',
				repairType:'',
				type:'',
				pid:'',
				allMaterialParts:[],
				formData:{
					wechatMaterialName:'',
					wechatMaterial:'',
					nums:1,
					maxNum:1
				},
				focusClass:{
					nums:false
				},
				allMaterialParts:[]
			}
		},
		onLoad(options){
			this.id = options.id;
			this.repairType = options.repairType;
			this.pid = options.repairType !== 'self' ? options.pid:'';
		},
		methods: {
			focus(e,key){
				this.focusClass[key] = true
			},
			blur(e,key){
				this.focusClass[key] = false
			},
			selectItem(e){
				this.formData.wechatMaterialName = e.name;
				this.formData.wechatMaterial = e.id;
				this.formData.maxNum = e.remain;
				this.formData.type = e.type;
			},
			getSearchAll(e){
				var postData ={
				      keywords:this.formData.wechatMaterialName
				    }
				    if (this.repairType!=='self'){
				      postData.pid = this.pid
				    }
				    this.getAllMaterialParts(postData)
			},
			/**获取所有部件类型*/
			getAllMaterialParts(data) {
				let self = this;
				self.$H.get(api.allMaterialParts,data).then(res=>{
					self.allMaterialParts=res.content;
				})
			},
			submit(){
				let self = this;
				if(self.formData.wechatMaterialName===''){
					uni.showToast({
						title:'请选择备件或零件'
					})
					return
				}
				if(self.formData.nums <= 0){
					uni.showToast({
						title:'数量不能小于0'
					})
					return
				}
				
				if(self.formData.nums===''){
					uni.showToast({
						title:'数量不能为空'
					})
					return
				}
				if(self.formData.nums > self.formData.maxNum){
					uni.showToast({
						title:'数量不能大于库存'
					})
					return
				}
				
				let postData = {
				    pid: self.id,
				    nums: self.formData.nums,
				    type:self.formData.type
				  };
				  uni.showLoading({
					  title:'提交中'
				  })
				self.formData.type === 1 ? postData.wechatMaterial = self.formData.wechatMaterial : postData.wechatParts = self.formData.wechatMaterial;
				switch(self.repairType){
					case 'self':
					self.$H.post(api.insertSelfRepairUse,postData).then(res=>{
						uni.showToast({
							title:'提交成功'
						})
						wx.navigateBack()
					})
					break;
					case 'send':
					self.$H.post(api.insertSendRepairUse,postData).then(res=>{
						uni.showToast({
							title:'提交成功'
						})
						wx.navigateBack()
					})
					break;
					case 'maintain':
					self.$H.post(api.insertMaintainRepairUse,postData).then(res=>{
						uni.showToast({
							title:'提交成功'
						})
						wx.navigateBack()
					})
					break;
				}
			}
		}
	}
</script>

<style>

</style>
